import requests
from tensorflow.contrib import slim
from tensorflow.contrib.slim.python.slim.nets.resnet_v1 import resnet_arg_scope, resnet_v1_101
import tensorflow.contrib.util as tfUtils
import cv2
from grpc.beta import implementations
import tensorflow as tf
from tensorflow.python.framework import dtypes
import time
from tensorflow.core.framework import types_pb2
from tensorflow_serving.apis import prediction_service_pb2, predict_pb2

batch_size = 32
height, width = 224, 224
url = 'http://127.0.0.1:8501/v1/models/resnet101:predict'
im_name = "/home/dolly/Desktop/img1.jpg"

# input
# input = X
# inputs = tf.random_uniform((batch_size, height, width, 3))
X = tf.placeholder(tf.float32, [None, height, width, 3])
Y = tf.placeholder(tf.float32, [None, 10])

# arg_scope = resnet_arg_scope()
# with slim.arg_scope(arg_scope):
#     net, end_points = resnet_v1_101(X, is_training=False)

sess = tf.Session()

# saver1 = tf.train.Saver(tf.global_variables())
# checkpoint_path = '/home/dolly/checkpoints/resnet_v1_101_2016_08_28/resnet_v1_101.ckpt'
# saver1.restore(sess, checkpoint_path)

# num_classes = 10
# net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='logits2')
# net = tf.squeeze(net, [1, 2], name='SpatialSqueeze')
# initializer
# init = tf.global_variables_initializer()
# sess.run(init)

# 对图片输入进行预处理
im = tf.read_file("/home/dolly/Desktop/img1.jpg")
im = tf.image.decode_jpeg(im)
im = tf.image.resize_images(im, (width, height))
im = tf.reshape(im, [-1, 224, 224, 3])
im = tf.cast(im, tf.float32)

images = sess.run(im)
images = images.tolist()
# 转化为numpy数组
# img_numpy = images.eval(session=sess)
# print("out2=", img_numpy)
# tmp = {'input': images}
# data = {'inputs': tmp}
data = {'inputs': {'input': images, 'shuchu': [1,1,1,1,1,1,1,1,1,1]}}
# data = {}
# print(len(tmp[0][0][0]))
# print(str(data))
# beginTime = time.time()

response = requests.post(url, json=data)
print(response.content)

# endTime = time.time()
# prediction = response.json()
# print(prediction)

# channel = implementations.insecure_channel("http://127.0.0.1", port=8501)
# stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
# request = predict_pb2.PredictRequest()
# 指定启动tensorflow serving时配置的model_name和是保存模型时的方法名
# request.model_spec.name = "resnet101"
# request.model_spec.signature_name = "serving"
# inputTensor = tensor = tf.contrib.util.make_tensor_proto(images, shape=[1, height, width, 3])
# request.inputs["input"].CopyFrom(inputTensor)
# timeout = 10.0
# response = stub.Predict(request, timeout)
# results = {}
# for key in response.outputs:
#     tensor_proto = response.outputs[key]
#     results[key] = tf.contrib.util.make_ndarray(tensor_proto)
# print(results)

# run
# images = sess.run(inputs)
# print(images)
# start_time = time.time()
# out_put = sess.run(net, feed_dict={X: images})

# 计算推理时间
# duration = time.time() - start_time

# reshape输出
# predict = tf.reshape(out_put, [-1, num_classes])
# max_idx_p = tf.argmax(predict, 1)
# print(out_put.shape)
# print(sess.run(max_idx_p))
# print('run time:', duration)